<?php

declare(strict_types=1);

namespace App\Admin;

use App\Entity\Feedback;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\AdminBundle\Templating\TemplateRegistry;
use Sonata\DoctrineORMAdminBundle\Filter\CallbackFilter;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

final class FeedbackAdmin extends AbstractAdmin {
    protected $baseRouteName = 'admin_feedback';
    protected $baseRoutePattern = 'feedback';
    protected $maxPageLinks = 10;
    protected $security;

    protected function configureDefaultSortValues (array &$sortValues) {
        $sortValues['_page'] = 1;
        $sortValues['_per_page'] = 32;
        $sortValues['_sort_order'] = 'DESC';
        $sortValues['_sort_by'] = 'id';
    }

    public function toString ($object) {
        return $object instanceof Feedback ? $object->getNickname() : '留言';
    }

    protected function configureDatagridFilters (DatagridMapper $datagridMapper): void {
        $datagridMapper
            ->add('nickname', null, ['label' => '昵称'])
            ->add('email', null, ['label' => '邮箱'])
            ->add('status', 'doctrine_orm_choice', ['label' => '状态'], ChoiceType::class, [
                'choices' => [
                    '未读' => 0,
                    '已读' => 1,
                ],
            ])
            ->add('created_at', CallbackFilter::class, [
                'label'    => '日期',
                'callback' => [$this, 'getCreatedAtFilter'],
            ], null, [
                'attr' => [
                    'readonly' => 'readonly',
                    'style'    => 'background-color: #fff',
                ],
            ]);
    }

    public function getCreatedAtFilter ($queryBuilder, $alias, $field, $value) {
        if (!$value['value']) {
            return false;
        }

        $dataArr = explode(' - ', $value['value']);
        if (count($dataArr) != 2) {
            return false;
        }

        $start = $dataArr[0].' 00:00:00';
        $end = $dataArr[1].' 23:59:59';
        $queryBuilder->andWhere($queryBuilder->expr()->between($alias.'.'.$field, "'".$start."'", "'".$end."'"));

        return true;
    }

    protected function configureListFields (ListMapper $listMapper): void {
        $listMapper
            ->add('nickname', null, [
                'label' => '昵称',
                'header_style' => 'width: 15%; text-align: left',
                'row_align'    => 'left',
            ])
            ->addIdentifier('content', null, [
                'label'        => '内容',
                'header_style' => 'width: 60%; text-align: left',
                'row_align'    => 'left',
                'template'     => 'backend/feedback/list_content.html.twig',
            ])
            ->add('status', TemplateRegistry::TYPE_CHOICE, [
                'label'        => '阅读',
                'choices'      => [
                    0 => '未读',
                    1 => '已读',
                ],
                'header_style' => 'width: 7%; text-align: left',
                'row_align'    => 'left',
            ])
            ->add('created_at', null, [
                'label'        => '日期',
                'format'       => 'Y-m-d H:i:s',
                'timezone'     => 'PRC',
                'header_style' => 'width: 13%; text-align: left',
                'row_align'    => 'left',
            ]);
    }

    protected function configureFormFields (FormMapper $formMapper): void {
        $formMapper
            ->add('id')
            ->add('nickname')
            ->add('email')
            ->add('content')
            ->add('ip')
            ->add('created_at')
            ->add('updated_at');
    }

    protected function configureShowFields (ShowMapper $showMapper): void {
        $showMapper
            ->add('id')
            ->add('nickname', null, ['label' => '昵称'])
            ->add('email', null, [
                'label'    => '邮箱',
                'template' => 'backend/feedback/show_email.html.twig',
            ])
            ->add('content', null, ['label' => '内容'])
            ->add('ip', null, ['label' => 'IP'])
            ->add('created_at', null, [
                'label'    => '日期',
                'format'   => 'Y-m-d H:i:s',
                'timezone' => 'PRC',
            ]);
    }

    protected function configureRoutes (RouteCollection $collection) {
        parent::configureRoutes($collection); // TODO: Change the autogenerated stub
        $collection->clearExcept(['list', 'show', 'delete', 'batch']);
    }

    public function getTemplate ($name) {
        switch ($name) {
            case 'list':
                return 'backend/feedback/list.html.twig';
            default:
                return $this->getTemplateRegistry()->getTemplate($name);
        }
    }

    protected function configureBatchActions ($actions) {
        // 批量已读操作
        $actions['read'] = [
            'label'            => '已读',
            'ask_confirmation' => true,
        ];
        return $actions;
    }
}
